package proj.unions.resource;

import org.json.JSONException;
import org.json.JSONObject;

import proj.unions.general.OnMyCallBack;
import proj.unions.general.SuperTools;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

public class InstallAPK  implements Runnable, RMConstDefine {
	
	private Activity mActivity = null;
	private OnMyCallBack mOnMyCallBack = null;
	private String mAPKPath = null;
	
	public InstallAPK(Activity a, OnMyCallBack l, JSONObject json)
	{
		mActivity = a;
		mOnMyCallBack = l;
		try {
			mAPKPath = json.getString(KEY_APK_PATH);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		if (mActivity == null || mOnMyCallBack == null)
		{
			mOnMyCallBack.onReceive("error", 0, null);
			return;
		}
		
		install();
	}
	
	private void install()
	{
		Intent i = new Intent(Intent.ACTION_VIEW);
		//fix bug:Android Pad上安装后直接关闭窗口
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.setDataAndType(Uri.parse("file://" + mAPKPath.toString()), "application/vnd.android.package-archive");
		mActivity.startActivity(i);
		
//		SuperTools.getInstance().showErrorDialog("安装新版本", "安装完请重启游戏。。。");
		SuperTools.getInstance().releaseGameRes();
	}
	
}